home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / misctext / dosref17 / chapter.008 < prev    next >
Text File  |  1990-03-20  |  45KB  |  768 lines

  1.  
  2.    **  Programmer's  Technical  Reference  for  MSDOS  and  the  IBM  PC **
  3.                         ┌─────────────────────────────┐
  4.                         │ Shareware Version, 03/21/90 │
  5.                         │  Please Register Your Copy  │
  6.                         └─────────────────────────────┘
  7.                     Copyright (c) 1987, 1990 Dave Williams
  8.  
  9.  
  10.                            C H A P T E R   E I G H T
  11.  
  12.                              DOS DISK INFORMATION
  13.  
  14.  
  15. THE DOS AREA├──────────────────────────────────────────────────────────────────
  16.  
  17.  All disks and diskettes formatted by DOS are created with a sector size of 512
  18. bytes. The DOS area (entire area for a diskette, DOS partition for hard disks)
  19. is formatted as follows:
  20.  
  21.            partition table          - variable size (hard disk only)
  22.            boot record              - 1 sector
  23.            first copy of the FAT    - variable size
  24.            second copy of the FAT   - same size as first copy
  25.            root directory           - variable size
  26.            data area                - variable depending on disk size
  27.  
  28. The following sections describe each of the allocated areas: 
  29.  
  30.  
  31. THE BOOT RECORD├───────────────────────────────────────────────────────────────
  32.  
  33.  The boot record resides on track 0, sector 1, side 0 of every diskette 
  34. formatted by the DOS FORMAT program. For hard disks the boot record resides on
  35. the first sector of the DOS partition. It is put on all disks to provide an
  36. error message if you try to start up with a nonsystem disk in drive A:. If the 
  37. disk is a system disk, the boot record contains a JMP instruction pointing to 
  38. the first byte of the operating system.
  39.  
  40.  If the device is IBM compatible, it must be true that the first sector of the 
  41. first FAT is located at the same sector for all possible media. This is 
  42. because the FAT sector is read before the media is actually determined.
  43.  The information relating to the BPB for a particular media is kept in the boot
  44. sector for the media. In particular, the format of the boot sector is:
  45.  
  46. ┌──────────────────────────────────────────────────────────────────────────────┐
  47. │                        D O S   B O O T   R E C O R D                         │
  48. ├───┬───────┬──────────────────────────────────────────────────────────────────┤
  49. │00h│3 bytes│  JMP to executable code. For DOS 2.x, 3 byte near jump (0E9h).   │
  50. │   │       │  For DOS 3.x, 2 byte near jump (0EBh) followed by a NOP (90h)    │
  51. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  52. │03h│8 bytes│  optional OEM name and version  (such as IBM 3.3)                │
  53. ├───┼───────┼─────┬────────────────────────────────────────────────────────────┤
  54. │0Dh│ byte  │     │  sectors per allocation unit (must be a power of 2)        │
  55. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  56. │0Eh│2 bytes│  B  │  reserved sectors (starting at logical sector 0)           │
  57. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  58. │10h│ byte  │     │  number of FATs                                            │
  59. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  60. │11h│2 bytes│     │  maximum number of root directory entries                  │
  61. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  62. │13h│2 bytes│  P  │  number of sectors in logical image (total number of       │
  63. │   │       │     │  sectors in media, including boot sector directories, etc.)│
  64. │   │       │     │  If logical disk size is geater than 32Mb, this value is 0 │
  65. │   │       │     │  and the actual size is reported at offset XXXX            │
  66. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  67. │15h│ byte  │  B  │  media descriptor byte                                     │
  68. ├───┼───────┤     ├────────────────────────────────────────────────────────────┤
  69. │16h│2 bytes│     │  number of sectors occupied by a single FAT                │
  70. ├───┼───────┼─────┴────────────────────────────────────────────────────────────┤
  71. │18h│2 bytes│  sectors per track                                               │
  72. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  73. │1Ah│2 bytes│  number of heads                                                 │
  74. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  75. │1Ch│2 bytes│  number of hidden sectors                                        │
  76. └───┴───────┼──────────────────────────────────────────────────────────────────┤
  77.             │                EXTENDED BOOT RECORD (DOS 4.0+)                   │
  78. ┌───┬───────┼──────────────────────────────────────────────────────────────────┤
  79. │1Eh│2 bytes│  number of sectors per track                                     │
  80. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  81. │20h│2 bytes│  number of heads                                                 │
  82. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  83. │22h│2 bytes│  number of hidden sectors                                        │
  84. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  85. │26h│4 bytes│  total number of sectors in media (32MB or larger indicated here)│
  86. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  87. │27h│ byte  │  physical drive number                                           │
  88. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  89. │28h│ byte  │  reserved                                                        │
  90. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  91. │29h│ byte  │  extended boot record signature                                  │
  92. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  93. │30h│4 bytes│  volume serial number (assigned with a random function)          │
  94. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  95. │34h│11bytes│  volume label                                                    │
  96. ├───┼───────┼──────────────────────────────────────────────────────────────────┤
  97. │3Fh│8 bytes│  reserved                                                        │
  98. └───┴───────┴──────────────────────────────────────────────────────────────────┘
  99.  
  100.  The three words at the end return information about the media. The number of
  101. heads is useful for supporting different multihead drives that have the same
  102. storage capacity but a different number of surfaces. The number of hidden
  103. sectors is useful for drive partitioning schemes.
  104.  
  105.  DOS 3.2 uses a table called the BIOS Parameter Block (BPB) to determine if a 
  106. disk has a valid File Allocation Table. The BPB is located in the first sector 
  107. of a floppy disk. Although the BPB is supposed to be on every formatted floppy 
  108. disk, some earlier versions of DOS did not create a BPB and instead assumed that
  109. the FAT begins at the second sector of the disk and that the first FAT byte 
  110. (Media Descriptor Byte) describes the disk format.
  111.  DOS 3.2 reads in the whole of the BPB and tries to use it - although,
  112. strangely enough, it seems as if DOS is prepared to cope with a BPB that is
  113. more or less totally blank (it seems to ignore the descriptor byte and treat it
  114. as a DSDD 9-sector disk).
  115.  DOS 3.2 determines if a disk has a valid boot sector by examining the first 
  116. byte of logical sector 0. If that byte it a jump instruction E9h, DOS 3.2 
  117. assumes the rest of the sector is a valid boot sector with a BPB. If the first 
  118. byte is not E9h DOS 3.2 behaves like previous versions, assumes the boot sector
  119. is invalid and uses the first byte of the FAT to determine the media type. 
  120.  If the first byte on the disk happens to be E9h, but the disk does not have a 
  121. BPB, DOS 3.2 will return a disk error message. 
  122.  The real problems occur if some of the BPB data is valid and some isn't.
  123. Apparently some OEMs have assumed that DOS would continue to ignore the
  124. formatting data on the disk, and have failed to write much there during FORMAT
  125. except the media descriptor byte (or, worse, have allowed random junk to be
  126. written there). While this error is understandable, and perhaps even
  127. forgiveable, it remains their problem, not IBMs, since the BPB area has always
  128. been documented as containing the format information that IBM DOS 3.2 now
  129. requires to be there. 
  130.  
  131.  
  132.  
  133. THE DOS FILE ALLOCATION TABLE (FAT)├───────────────────────────────────────────
  134.                                                         
  135.  The File Allocation Table, or FAT, has three pain purposes:
  136.         1) to mark bad sectors on the media
  137.         2) to determine which sectors are free for use
  138.         3) to determine the physical location(s) of a file on the media.
  139.  
  140.  DOS uses one of four different schemes for defining the File Allocation Table:
  141.         1) the 12-bit FAT, for DOS 1.x, 2.x, all floppies, and small hard disks
  142.         2) the 16-bit FAT, for DOS 3.x+ hard disks from 16.8 to 32Mb
  143.         3) the 32-bit FAT used by Compaq DOS 3.31 and others
  144.         4) the 32-bit FAT used by DOS 4.0 over-32Mb logical drives
  145.  
  146.  This section explains how DOS uses the FAT to convert the clusters of a file 
  147. into logical sector numbers. We recommend that system utilities use the DOS 
  148. handle calls rather than interpreting the FAT.
  149.  
  150.  The FAT is used by DOS to allocate disk space for files, one cluster at a time.
  151. In DOS 4.0, clusters are referred to as "allocation units." It means the same 
  152. things; the smallest logical portion of a drive.
  153.  
  154.  The FAT consists of a 12 bit entry (1.5 bytes) for each cluster on the disk or
  155. a 16 bit (2 bytes) entry when a hard disk has more than 20740 sectors as is the
  156. case with fixed disks larger than 10Mb.
  157.  The first two FAT entries map a portion of the directory; these FAT entries 
  158. contain indicators of the size and format of the disk. The FAT can be in a 12 
  159. or 16 bit format. DOS determines whether a disk has a 12 or 16 bit FAT by 
  160. looking at the total number of allocation units on a disk. For all diskettes 
  161. and hard disks with DOS partitions less than 20,740 sectors, the FAT uses a 12 
  162. bit value to map a cluster. For larger partitions, DOS uses a 16 bit value.
  163.  The second, third, and fourth bit applicable for 16 bit FAT bytes always 
  164. contains 0FFFFh. The first byte is used as follows:
  165.  
  166. ┌──────────────────────────────────────────────────────────────────────────────┐
  167. │                 M E D I A    D E S C R I P T O R    B Y T E                  │
  168. ├──────────┬──────────────────────────────────┬────────────────────────────────┤
  169. │hex value │              meaning             │         normally used          │
  170. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  171. │    F0    │ double sided  18 sector diskette │ PS/2 1.44 meg DSHD             │
  172. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  173. │    F8    │ hard disk                        │ bootable hard disk at C:800    │
  174. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  175. │    F9    │ double sided  15 sector diskette │ AT 1.2 meg DSHD                │
  176. │          │ double sided  9  sector diskette │ Convertible 720k DSQD          │
  177. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  178. │    FA    │ IBM Displaywriter System disk    │ 287k                           │
  179. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  180. │    FB    │ IBM Displaywriter System disk    │ 1 meg                          │
  181. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  182. │    FC    │ single sided  9  sector diskette │ DOS 2.0, 180k SSDD             │
  183. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  184. │    FD    │ double sided  9  sector diskette │ DOS 2.0, 360k DSDD             │
  185. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  186. │    FE    │ single sided  8  sector diskette │ DOS 1.0, 160k SSDD             │
  187. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  188. │    FF    │ double sided  8  sector diskette │ DOS 1.1, 320k SSDD             │
  189. ├──────────┴───────────┬──────────────────────┴────────────────────────────────┘
  190. │for 8 inch diskettes: │
  191. ├──────────┬───────────┴──────────────────────┬────────────────────────────────┐
  192. │    FD    │ double sided  26 sector diskette │ IBM 3740 format DSSD           │
  193. ├──────────┼──────────────────────────────────┼────────────────────────────────┤
  194. │    FE    │ single sided  26 sector diskette │ IBM 3740 format SSSD           │
  195. │          ├──────────────────────────────────┼────────────────────────────────┤
  196. │          │ double sided  8  sector diskette │ IBM 3740 format DSDD           │
  197. └──────────┴──────────────────────────────────┴────────────────────────────────┘
  198.  
  199.  
  200. The third FAT entry begins mapping the data area (cluster 002).
  201.  
  202. NOTE: These values are provided as a reference. Therefore, programs should not 
  203.       make use of these values.
  204.  
  205.  Each entry contains a hexadecimal character (or 4 for 16 bit FATs). () 
  206. indicates the high order four bit value in the case of 16 bit FAT entries. 
  207. They can be:
  208.  
  209.             (0)000h  if the cluster is unused and availible
  210.  
  211. (0F)FF8h - (0F)FFFh  to indicate the last cluster of a file
  212.  
  213.             (X)XXXh  any other hexadecimal numbers that are the cluster number
  214.                      of the next cluster in the file. The cluster number is the
  215.                      first cluster in the file that is kept in the file's 
  216.                      directory entry.
  217.  
  218.  The values (0F)FF0h - (0F)FF7h are used to indicate reserved clusters. 
  219. (0F)FF7h indicates a bad cluster if it is not part of the allocation chain. 
  220. (0F)FF8h - (0F)FFFh are used as end of file markers.
  221.  
  222.  The file allocation table always occupies the sector or sectors immediately 
  223. following the boot record. If the FAT is larger than 1 sector, the sectors 
  224. occupy consecutive sector numbers. Two copies of the FAT are written, one 
  225. following the other, for integrity. The FAT is read into one of the DOS buffers
  226. whenever needed (open, allocate more space, etc).
  227.  
  228.  
  229.  
  230. USE OF THE 12 BIT FILE ALLOCATION TABLE├───────────────────────────────────────
  231.  
  232. Obtain the starting cluster of the file from the directory entry.
  233.  
  234. Now, to locate each subsequent sector of the file:
  235.  
  236. 1. Multiply the cluster number just used by 1.5 (each FAT entry is 1.5 
  237.    bytes long).
  238. 2. The whole part of the product is offset into the FAT, pointing to the entry 
  239.    that maps the cluster just used. That entry contains the cluster number of 
  240.    the next cluster in the file.
  241. 3. Use a MOV instruction to move the word at the calculated FAT into a register.
  242. 4. If the last cluster used was an even number, keep the low order 12 bits of 
  243.    the register, otherwise, keep the high order 12 bits.
  244. 5. If the resultant 12 bits are (0FF8h-0FFFh) no more clusters are in the file.
  245.    Otherwise, the next 12 bits contain the cluster number of the next cluster in
  246.    the file. 
  247.  
  248.   To convert the cluster to a logical sector number (relative sector, such as 
  249. that used by int 25h and 26h and DEBUG):
  250.  
  251. 1. Subtract 2 from the cluster number
  252. 2. Multiply the result by the number of sectors per cluster.
  253. 3. Add the logical sector number of the beginning of the data area.
  254.  
  255. 12-bit FAT if DOS partition is smaller than 32,680 sectors (16.340 MB).
  256.  
  257.  
  258. USE OF THE 16 BIT FILE ALLOCATION TABLE├───────────────────────────────────────
  259.  
  260.  Obtain the starting cluster of the file from the directory entry. Now to 
  261. locate each subsequent cluster of the file:
  262.  
  263. 1.  Multiply the cluster number used by 2 (each FAT entry is 2 bytes long).
  264. 2.  Use the MOV word instruction to move the word at the calculated FAT offset 
  265.     into a register.
  266. 3.  If the resultant 16 bits are (0FF8h-0FFFFh) no more clusters are in the 
  267.     file. Otherwise, the 16 bits contain the cluster number of the next cluster 
  268.     in the file. 
  269.  
  270.  Compaq DOS makes availible a new disk type (6) with 32 bit partition values, 
  271. allowing 512 megabytes per hard disk (Compaq DOS 3.3.1)
  272.  
  273.  
  274. USE OF THE 32 BIT FILE ALLOCATION TABLE├───────────────────────────────────────
  275.  
  276.  DOS 4.0's FORMAT program checks the size of the availible media. If the total 
  277. number of 8 sector clusters is less than 4086 (not 4096) a 12 bit FAT is used.
  278.  
  279.  
  280.  
  281. DOS DISK DIRECTORY├────────────────────────────────────────────────────────────
  282.  
  283.  The FORMAT command initially builds the root directory for all disks. Its 
  284. location (logical sector number) and the maximum number of entries are 
  285. availible through the device driver interfaces.
  286.  
  287.  Since directories other than the root directory are actually files, there is 
  288. no limit to the number of entries that they may contain. 
  289.  
  290.  All directory entries are 32 bytes long, and are in the following format:
  291. ┌───────┬──────────────────────────────────────────────────────────────────────
  292. │ BYTES │                      DISK DIRECTORY ENTRY
  293. ├───────┼──────────────────────────────────────────────────────────────────────
  294. │  0-7  │ 8 bytes filename
  295. └───────┼──────────────────────────────────────────────────────────────────────
  296.         │ The first byte of the filename indicates the file status.
  297.         │ The file status byte can contain the following values:
  298.         ├──────┬───────────────────────────────────────────────────────────────
  299.         │  00h │ Directory entry has never been used. This is used to limit
  300.         │      │ the length of directory searches, for performance reasons.
  301.         │  05h │ Indicates that the first character of the filename actually
  302.         │      │ has an 0EDh character.
  303.         │ 0E5h │ Filename has been used but the file has been erased.
  304.         │  2Eh │ This entry is for a directory. If the second byte is also
  305.         │      │ 2Eh, the cluster field contains the cluster number of this
  306.         │      │ directory's parent directory. (0000h if the parent directory
  307.         │      │ is the root directory).
  308.         ├──────┴───────────────────────────────────────────────────────────────
  309.         │ Any other character is the first character of a filename. Filenames
  310.         │ are left-aligned and if nescessary padded with blanks.
  311. ┌───────┼──────────────────────────────────────────────────────────────────────
  312. │  8-A  │ 3 bytes filename extension if any
  313. └───────┼──────────────────────────────────────────────────────────────────────
  314.         │ Three characters, left-aligned and padded with blanks if nescessary.
  315.         │ If there is no file extension, this field contains all blanks
  316. ┌───────┼──────────────────────────────────────────────────────────────────────
  317. │   B   │ 1 byte file attributes
  318. └───────┼──────────────────────────────────────────────────────────────────────
  319.         │  The attribute byte is mapped as follows:
  320.         ├─────┬───┬────────────────────────────────────────────────────────────
  321.         │ hex │bit│                        meaning
  322.         ├─────┼───┼────────────────────────────────────────────────────────────
  323.         │ 00h │   │ (no bits set) normal; can be read or written w/o restriction
  324.         │ 01h │ 0 │ file is marked read-only. An attempt to open the file for
  325.         │     │   │ output using function call 3Dh will fail and an error code
  326.         │     │   │ will be returned. This value can be used with other values
  327.         │     │   │ below.
  328.         │ 02h │ 1 │ indicates a hidden file. The file is excluded from normal
  329.         │     │   │ directory searches.
  330.         │ 04h │ 2 │ indicates a system file. This file is excluded from normal
  331.         │     │   │ directory searches.
  332.         │ 08h │ 3 │ indicates that the entry contains the volume label in the
  333.         │     │   │ first 11 bytes. The entry has no other usable information
  334.         │     │   │ and may exist only in the root directory.
  335.         │ 10h │ 4 │ indicates that the file is a subdirectory
  336.         │ 20h │ 5 │ indicates an archive bit. This bit is set on whenever the
  337.         │     │   │ file is written to and closed. Used by BACKUP and RESTORE.
  338.         │     │ 6 │ reserved
  339.         │     │ 7 │ reserved
  340.         ├─────┴───┴────────────────────────────────────────────────────────────
  341.         │ Bits 6 and 7 are reserved, and must be 0. They may be used in OS/2.
  342.         │ note: Attributes 08h and 10h cannot be changed using int21/43h.
  343.         │
  344.         │ The system files IBMBIO.COM and IBMDOS.COM (or customized equivalent)
  345.         │ are marked as read-only, hidden, and system files. Files can be
  346.         │ marked hidden when they are created. Also, the read-only, hidden,
  347.         │ system and archive attributes may be changed with int21/43h.
  348. ┌───────┼──────────────────────────────────────────────────────────────────────
  349. │ 0C-15 │ 10 bytes reserved by DOS; value unknown
  350. ├───────┼──────────────────────────────────────────────────────────────────────
  351. │ 16-17 │ 2 bytes file timestamp
  352. └───────┼──────────────────────────────────────────────────────────────────────
  353.         │ These bytes contain the time when the file was created or last
  354.         │  updated. The time is mapped in the bits as follows:
  355.         ├───────────────────────────────┬───────────────────────────────┐
  356.         │         B Y T E   16h         │         B Y T E   17h         │
  357.         ├───────────────────────────────┼───────────────────────────────┤
  358.         │ F   E   D   C   B   A   9   8 │ 7   6   5   4   3   2   1   0 │
  359.         ├───────────────────┬───────────┴───────────┬───────────────────┤
  360.         │ H   H   H   H   H │ M   M   M   M   M   M │ D   D   D   D   D │
  361.         ├───────────────────┼───────────────────────┼───────────────────┤
  362.         │ binary # hrs 0-23 │ binary # minutes 0-59 │ bin. # 2-sec incr │
  363.         ├───────────────────┴───────────────────────┴───────────────────┘
  364.         │ note: The time is stored with the least significant byte first.
  365. ┌───────┼──────────────────────────────────────────────────────────────────────
  366. │ 18-19 │ 2 bytes file datestamp
  367. └───────┼──────────────────────────────────────────────────────────────────────
  368.         │ This area contains the date when the file was created or last
  369.         │ updated. The mm/dd/yy are mapped in the bits as follows:
  370.         ├───────────────────────────────┬───────────────────────────────┐
  371.         │         B Y T E   18h         │         B Y T E   19h         │
  372.         ├───────────────────────────────┼───────────────────────────────┤
  373.         │ F   E   D   C   B   A   9   8 │ 7   6   5   4   3   2   1   0 │
  374.         ├───────────────────────────┬───┴───────────┬───────────────────┤
  375.         │ Y   Y   Y   Y   Y   Y   Y │ M   M   M   M │ D   D   D   D   D │
  376.         ├───────────────────────────┼───────────────┼───────────────────┤
  377.         │     0-119 (1980-2099)     │     1-12      │       1-31        │
  378.         ├───────────────────────────┴───────────────┴───────────────────┘
  379.         │ note: The time is stored with the least significant byte first.
  380. ┌───────┼──────────────────────────────────────────────────────────────────────
  381. │ 1A-1B │ 2 bytes first file cluster number 
  382. └───────┼──────────────────────────────────────────────────────────────────────
  383.         │ * (reserved in DOS 2, documented in DOS 3+)
  384.         │ This area contains the starting cluster number of the first cluster
  385.         │ in the file. The first cluster for data space on all fixed disks and
  386.         │ floppy disks is always cluster 002. The cluster number is stored
  387.         │ with the least significant byte first.
  388. ┌───────┼──────────────────────────────────────────────────────────────────────
  389. │ 1C-1F │ 4 bytes filesize
  390. └───────┼──────────────────────────────────────────────────────────────────────
  391.         │ This area contains the file size in bytes. The first word contains
  392.         │ the low order part of the size. Both words are stored with the least
  393.         │ significant byte first.
  394.         └──────────────────────────────────────────────────────────────────────
  395.  
  396.  
  397.  
  398. THE DATA AREA├─────────────────────────────────────────────────────────────────
  399.  
  400.  Allocation of space for a file (in the data area) is done only when needed 
  401. (it is not preallocated). The space is allocated one cluser (unit allocation) 
  402. at a time. A cluster is always one or more consecutive sector numbers, and all 
  403. of the clusters in a file are "chained" together in the FAT. 
  404.  
  405.  The clusters are arranged on disk to minimize head movement for multisided 
  406. media. All of the space on a track (or cylinder) is allocated before moving 
  407. on to the next track. This is accomplished by using the sequential sector 
  408. numbers on the lowest-numbered head, then all the sector numbers on the next 
  409. head, and so on until all sectors of all heads of the track are used. Then the 
  410. next sector used will be sector 1 of head 0 on the next track.
  411.  
  412.  An interesting innovation that was introduced in MS-DOS 3.0: disk space that 
  413. is freed by erasing a file is not re-used immediately, unlike earlier versions 
  414. of DOS. Instead, free space is obtained from the area not yet used during the 
  415. current session, until all of it is used up. Only then will space that is freed
  416. during the current session be re-used.
  417.  
  418.  This feature minimizes fragmentation of files, since never-before-used space
  419. is always contiguous. However, once any space has been freed by deleting a file,
  420. that advantage vanishes at the next system boot. The feature also greatly
  421. simplifies un-erasing files, provided that the need to do an un-erase is found
  422. during the same session and also provided that the file occupies contiguous 
  423. clusters.
  424.  
  425.  However, when one is using programs which make extensive use of temporary
  426. files, each of which may be created and erased many times during a session,
  427. the feature becomes a nuisance; it forces the permanent files to move farther
  428. and farther into the inner tracks of the disk, thus increasing rather than
  429. decreasing the amount of fragmentation which occurs.
  430.  
  431.  The feature is implemented in DOS by means of a single 16-bit "last cluster
  432. used" (LCU) pointer for each physical disk drive; this pointer is a part of
  433. the physical drive table maintained by DOS. At boot time, the LCU pointer is
  434. zeroed. Each time another cluster is obtained from the free-space pool (the
  435. FAT), its number is written into the LCU pointer. Each time a fresh cluster
  436. is required, the FAT is searched to locate a free one; in older versions of
  437. DOS this search always began at Cluster 0000, but in 3.x it begins at the
  438. cluster pointed to by the LCU pointer.
  439.  
  440.  For hard disks, the size of the file allocation table and directory are 
  441. determined when FORMAT initializes it and are based on the size of the DOS 
  442. partition.
  443.  
  444.  Files in the data area are not nescessarily written sequentially on the first.
  445. The data area space is allocated one cluster at a time, skipping over clusters 
  446. already allocated. The first free cluster found is the next cluster allocated, 
  447. regardless of its physical location on the disk. This permits the most efficient
  448. utilization of disk space because clusters freed by erasing files can be 
  449. allocated for new files. Refer back to the description of the DOS FAT in this 
  450. chapter for more information.
  451.  
  452.         SSDD    single sided, double density    (160-180k)     5¼
  453.         DSDD    double sided, double density    (320-360k)     5¼
  454.         DSQD    double sided, quad density      (720k)         5¼, 3½
  455.         DSHD    double sided, high density      (1.2-1.44M)    5¼, 3½
  456.  
  457.  Much of the trouble with AT 1.2 meg drives has been through the inadverdent 
  458. use of quad density disks in the high density drives. The high density disks 
  459. use a higher-coercivity media than the quads, and quads are not completely 
  460. reliable as 1.2M. Make sure you have the correct disk for your application.
  461.      
  462.  
  463.  
  464. HARD DISK LAYOUT├──────────────────────────────────────────────────────────────
  465.  
  466.  The DOS hard disk routines perform the following services:
  467.  
  468. 1) Allow multiple operating systems to utilize the hard disk without the need 
  469.    to backup and restore files when changing operating systems.
  470.  
  471. 2) Allow a user-selected operating system to be started from the hard disk.
  472.    
  473.    I) In order to share the hard disk among operating systems, the disk may be 
  474.       logically divided into 1 to 4 partitions. The space within a given 
  475.       partition is contiguous, and can be dedicated to a specific operating 
  476.       system. Each operating system may "own" only one partition in DOS versions
  477.       2.0 through 3.2. PCDOS 3.3 introduced the "Extended DOS Partition" which 
  478.       allows multiple DOS partitions on the same hard disk. The FDISK.COM (or 
  479.       similar program from other DOS vendors) utility allows the user to select
  480.       the number, type, and size of each partition. The partition information is
  481.       kept in a partition table that is embedded in the master fixed disk boot 
  482.       record on the first sector of the disk. The format of this table varies 
  483.       from version to version of DOS.
  484.  
  485.   II) An operating system must consider its partition to be the entire disk, 
  486.       and must ensure that its functions and utilities do not access other 
  487.       partitions on the disk. 
  488.  
  489.  III) Each partition may contain a boot record on its first sector, and any 
  490.       other programs or data that you choose - including a copy of an operating 
  491.       system. For example, the DOS FORMAT command may be used to format and 
  492.       place a copy of DOS in the DOS partition in the same manner that a 
  493.       diskette is formatted. With the FDISK utility, you may designate a 
  494.       partition as "active" (bootable). The master hard disk boot record causes
  495.       that partition's boot record to receive control when the system is started
  496.       or reset. Additional disk partitions could be FORTH, UNIX, Pick, CP/M-86,
  497.       or the UCSD p-System.
  498.  
  499.  
  500.  
  501. SYSTEM INITIALIZATION├─────────────────────────────────────────────────────────
  502.  
  503. The boot sequence is as follows:
  504.  
  505. 1. System initialization first attempts to load an operating system from 
  506.    diskette drive A. If the drive is not ready or a read error occurs, it then 
  507.    attempts to read a master hard disk boot record on the first sector of the 
  508.    first hard disk in the system. If unsuccessful, or if no hard disk is 
  509.    present, it invokes ROM BASIC in an IBM PC or displays a disk error 
  510.    message on most compatibles.
  511.  
  512. 2. If initialization is successful, the master hard disk boot record is given 
  513.    control and it examines the partition table embedded within it. If one of 
  514.    the entries indicates an active (bootable) partition, its boot record is 
  515.    read from the partition's first sector and given control.
  516.  
  517. 3. If none of the partitions is bootable, ROM BASIC is invoked on an IBM PC or
  518.     a disk error on most compatibles.
  519.  
  520. 4. If any of the boot indicators are invalid, or if more than one indicator is 
  521.    marked as bootable, the message INVALID PARTITION TABLE is displayed and the 
  522.    system stops.
  523.  
  524. 5. If the partition's boot record cannot be successfully read within five 
  525.    retries due to read errors, the message ERROR LOADING OPERATING SYSTEM 
  526.    appears and the system stops.
  527.  
  528. 6. If the partition's boot record does not contain a valid "signature", the 
  529.    message MISSING OPERATING SYSTEM appears, and the system stops.
  530.  
  531. NOTE: When changing the size or location of any partition, you must ensure that
  532.       all existing data on the disk has been backed up. The partitioning program
  533.       will destroy the data on the disk.
  534.  
  535.  System programmers designing a utility to initialize/manage a hard disk must 
  536. provide the following functions at a minimum:
  537.  
  538. 1. Write the master disk boot record/partition table to the disk's first 
  539.    sector to initialize it.
  540.  
  541. 2. Perform partitioning of the disk-that is, create or update the partition 
  542.    table information (all fields for the partition) when the user wishes 
  543.    to create a partition. This may be limited to creating a partition for only 
  544.    one type of operating systewm, but must allow repartitoning the entire disk,
  545.    or adding a partition without interfering with existing partitions (user's 
  546.    choice).
  547.  
  548. 3. Provide a means for marking a user-specified partition as bootable and 
  549.    resetting the bootable indicator bytes for all other partitions at the same 
  550.    time.
  551.  
  552. 4. Such utilities should not change or move any partition information that 
  553.    belongs to another operating system.
  554.  
  555.  
  556.  
  557.  
  558. BOOT RECORD/PARTITION TABLE├───────────────────────────────────────────────────
  559.  
  560.  A boot record must be written on the first sector of all hard disks, and 
  561. must contain the following:
  562.  
  563. 1. Code to load and give control to the boot record for one of four possible 
  564.    operating systems.
  565.  
  566. 2. A partition table at the end of the boot record. Each table entry is 16 
  567.    bytes long, and contains the starting and ending cylinder, sector, and head 
  568.    for each of four possible partitions, as well as the number of sectors 
  569.    preceding the partition and the number of sectors occupied by the partition. 
  570.    The "boot indicator" byte is used by the boot record to determine if one of 
  571.    the partitions contains a loadable operating system. FDISK initialization 
  572.    utilities mark a user-selected partition as "bootable" by placing a value 
  573.    of 80h in the corresponding partition's boot indicator (setting all other 
  574.    partitions' indicators to 0 at the same time). The presence of the 80h tells 
  575.    the standard boot routine to load the sector whose location is contained in 
  576.    the following three bytes. That sector is the actual boot record for the 
  577.    selected operating system, and it is responsible for the remainder of the 
  578.    system's loading process (as it is from the diskette). All boot records are 
  579.    loaded at absolute address 0:7C00.
  580.  
  581. The partition table with its offsets into the boot record is:
  582. (except for Wyse DOS 3.2 with 32 bit allocation table, and DOS 3.3-up)
  583. ┌──────┬────────┬──────────────────┬──────────┬──────────┬──────────┬──────────┐
  584. │Offset│Partit'n│     Purpose      │          │   Head   │  Sector  │ Cylinder │
  585. ├──────┼────────┼──────────────────┼──────────┼──────────┼──────────┼──────────┤
  586. │ 1BEh │ part 1 │ begin            │ boot ind │    H     │    S     │   cyl    │
  587. ├──────┼────────┼──────────────────┼──────────┼──────────┼──────────┼──────────┤
  588. │ 1C2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  589. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  590. │ 1C6h │        │ relative sector  │      low word       │      high word      │
  591. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  592. │ 1CAh │        │ # sectors        │      low word       │      high word      │
  593. ├──────┼────────┼──────────────────┼──────────┬──────────┼──────────┬──────────┤
  594. │ 1CEh │ part 2 │ begin            │ boot ind │    H     │    S     │   cyl    │
  595. ├──────┤        ├──────────────────┼──────────┼──────────┼──────────┼──────────┤
  596. │ 1D2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  597. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  598. │ 1D6h │        │ relative sector  │      low word       │      high word      │
  599. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  600. │ 1DAh │        │ # sectors        │      low word       │      high word      │
  601. ├──────┼────────┼──────────────────┼──────────┬──────────┼──────────┬──────────┤
  602. │ 1DEh │ part 3 │ begin            │ boot ind │    H     │    S     │   cyl    │
  603. ├──────┤        ├──────────────────┼──────────┼──────────┼──────────┼──────────┤
  604. │ 1E2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  605. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  606. │ 1E6h │        │ relative sector  │      low word       │      high word      │
  607. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  608. │ 1EAh │        │ # sectors        │      low word       │      high word      │
  609. ├──────┼────────┼──────────────────┼──────────┬──────────┼──────────┬──────────┤
  610. │ 1EEh │ part 4 │ begin            │ boot ind │    H     │    S     │   cyl    │
  611. ├──────┤        ├──────────────────┼──────────┼──────────┼──────────┼──────────┤
  612. │ 1F2h │        │ end              │ syst ind │    H     │    S     │   cyl    │
  613. ├──────┤        ├──────────────────┼──────────┴──────────┼──────────┴──────────┤
  614. │ 1F6h │        │ relative sector  │      low word       │      high word      │
  615. ├──────┤        ├──────────────────┼─────────────────────┼─────────────────────┤
  616. │ 1FAh │        │ # sectors        │      low word       │      high word      │
  617. ├──────┼────────┴──────────────────┼──────────┬──────────┼─────────────────────┘
  618. │ 1FEh │         signature         │  hex 55  │  hex AA  │
  619. └──────┴───────────────────────────┴──────────┴──────────┘
  620.  
  621.  
  622.  Boot indicator (boot ind): The boot indicator byte must contain 0 for a non- 
  623. bootable partition or 80h for a bootable partition. Only one partition can be 
  624. marked as bootable at a time.
  625.  
  626.  System Indicator (sys ind): The sys ind field contains an indicator of the 
  627. operating system that "owns" the partition. IBM PC-DOS can only "own" one 
  628. partition, though some versions of MSDOS allow all four partitions to be used 
  629. by DOS. 
  630.  The system indicators are:
  631.  
  632.         ┌─────────────────────────────────────────────────────────────┐
  633.         │                 System Indicator  (sys ind)                 │
  634.         ├───────┬─────────────────────────────────────────────────────┤
  635.         │  00h  │  unknown or unspecified                             │
  636.         ├───────┼─────────────────────────────────────────────────────┤
  637.         │  01h  │  DOS 12 bit FAT  (DOS 2.x all and 3.x under 10 Mb)  │
  638.         ├───────┼─────────────────────────────────────────────────────┤
  639.         │  04h  │  DOS 16 bit FAT  (DOS 3.x+. Not recognized by 2.x)  │
  640.         ├───────┼─────────────────────────────────────────────────────┤
  641.         │  0DBh │  DRI Concurrent DOS                                 │
  642.         ├───────┼─────────────────────────────────────────────────────┤
  643.         │  0F2h │  2nd DOS partition, some 3.2 and all 3.3            │
  644.         └───────┴─────────────────────────────────────────────────────┘
  645.  
  646.  There are bytes for XENIX, and other operating systems. Some manufacturers
  647. (such as Zenith, Wyse, and Tandon) diddle with these system bytes to implement
  648. more than one DOS partition per disk.
  649.  
  650.  
  651.  Cylinder (CYL) and Sector (S): The 1 byte fields labelled CYL contain the low 
  652. order 8 bits of the cylinder number - the high order 2 bits are in the high 
  653. order 2 bits of the sector (S) field. This corresponds with the ROM BIOS 
  654. interrupt 13h (disk I/O) requirements, to allow for a 10 bit cylinder number.
  655.  
  656.  The fields are ordered in such a manner that only two MOV instructions are 
  657. required to properly set up the DX and CX registers for a ROM BIOS call to 
  658. load the appropriate boot record (hard disk booting is only possible from the 
  659. first hard disk in the system, where a BIOS drive number of 80h corresponds 
  660. to the boot indicator byte).
  661.  
  662.  All partitions are allocated in cylinder multiples and begin on sector 1,
  663. head 0, with the exception that the partition that is allocated at the beginning
  664. of the disk starts at sector 2, to account for the hard disk's master boot
  665. record.
  666.  
  667.  Relative Sector (rel sect): The number of sectors preceding each partition 
  668. on the disk is kept in the 4 byte field labelled "rel sect". This value is 
  669. obtained by counting the sectors beginning with cylinder 0, sector 1, head 0 
  670. of the disk, and incrementing the sector, head, and then track values up to 
  671. the beginning of the partition. This, if the disk has 17 sectors per track and 
  672. 4 heads, and the second partition begins at cylinder 1, sector 1, head 0,and 
  673. the partition's starting relative sector is 68 (decimal) - there were 17 
  674. sectors on each of 4 heads on 1 track allocated ahead of it. The field is stored
  675. with the least significant word first.
  676.  
  677.  Number of sectors (#sects): The number of sectors allocated to the partition 
  678. is kept in the "# of sects" field. This is a 4 byte field stored least 
  679. significant word first.
  680.  
  681.  Signature: The last 2 bytes of the boot record (55AAh) are used as a signature
  682. to identify a valid boot record. Both this record and the partition boot record 
  683. are required to contain the signature at offset 1FEh.
  684.  
  685.  
  686. HARD DISK TECHNICAL INFORMATION├───────────────────────────────────────────────
  687.  
  688.  Western Digital's hard disk installation manuals make the claim that MSDOS 
  689. can support only 2 hard drives. This is entirely false, and their purpose for 
  690. making the claim is unclear. DOS merely performs a function call pointed at 
  691. the hard disk driver, which is normally in one of three locations; a ROM at 
  692. absolute address C:800, the main BIOS ROM if the machine is an AT, or a device 
  693. driver installed through the CONFIG.SYS file. Two hard disk controller cards 
  694. can normally not reside in the same machine due to lack of interrupt 
  695. arbitration. Perstor's ARLL controller and some cards marketed by Novell can 
  696. coexist with other controllers. Perstor's technical department has had four 
  697. controllers and eight hard disks in the same IBM XT functioning concurrently.
  698.  
  699.  A valid hard disk has a boot record arranged in the following manner:
  700.  
  701.         DB      drive   ; 0 or 80h  (80h marks a bootable, active partition)
  702.         DB      head1   ; starting heads
  703.         DW      trksec1 ; starting track/sector (CX value for INT 13)
  704.         DB      system  ; see below
  705.         DB      head2   ; ending head
  706.         DW      trksec2 ; ending track/sector
  707.         DD      sector1 ; absolute # of starting sector
  708.         DD      sector2 ; absolute # of last sector
  709.  
  710.  The master disk boot record invokes ROM BASIC if no indicator byte reflects a 
  711. bootable system.
  712.  
  713.  When a partition's boot record is given control, it is passed its partition 
  714. table entry address in the DS:SI registers.
  715.  
  716.  
  717.  
  718. DETERMINING FIXED DISK ALLOCATION├─────────────────────────────────────────────
  719.  
  720. DOS determines disk allocation using the following formula:
  721.               
  722.                                          D * BPD
  723.                             TS - RS -  ───────────
  724.                                            BPS
  725.                       SPF = ──────────────────────────────
  726.                                         BPS * SPC
  727.                                  CF + ──────────────
  728.                                            BPC
  729. where:
  730.  
  731.      TS      total sectors on disk
  732.      RS      the number of sectrs at the beginning of the disk that are reserved
  733.              for the boot record. DOS reserves 1 sector.
  734.      D       The number of directory entries in the root directory.
  735.      BPD     the number of bytes per directory entry. This is always 32.
  736.      BPS     the number of bytes per logical sector. Typically 512, but you can
  737.              specify a different number with VDISK.
  738.      CF      The number of FATS per disk. Usually 2. VDISK is 1.
  739.      SPF     the number of sectors per FAT. Maximum 64.
  740.      SPC     The number of sectors per allocation unit.
  741.      BPC     the number of bytes per FAT entry. BPC is 1.5 for 12 bit FATs.
  742.              2 for 16 bit FATS.
  743.  
  744.  
  745. To calculate the minimum partition size that will force a 16-bit FAT:
  746.  
  747.         CYL = (max clusters * 8)/(HEADS * SPT)
  748.  
  749. where:
  750.      CYL     number of cylinders on the disk
  751.      max clusters  4092 (maximum number of clusters for a 12 bit FAT)
  752.      HEADS   number of heads on the hard disk
  753.      SPT     sectors per track  (normally 17 on MFM)
  754.  
  755.  
  756.  DOS 2.0 uses a "first fit" algorithm when allocating file space on the hard
  757. disk. Each time an application requests disk space, it will scan from the 
  758. beginning of the FAT until it finds a contiguous piece of storage large enough 
  759. for the file.
  760.  DOS 3.0 keeps a pointer into the disk space, and begins its search from the 
  761. point it last left off. This pointer is lost when the system is rebooted. 
  762. This is called the "next fit" algorithm. It is faster than the first fit and 
  763. helps minimize fragmentation.
  764.  In either case, if the FCB function calls are used instead of the handle 
  765. function calls, the file will be broken into pieces starting with the first 
  766. availible space on the disk.
  767.  
  768.